package com.bo.entity;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.content.res.XmlResourceParser;
import android.util.Xml;
import com.bo.util.LoadResourceProxy;


public class ProductList {
	

	public List<Product> createProductList(String companyId,int page,int offset) throws Exception{
		
		List<Product> plist=new ArrayList<Product>();
		
        String xmlStr=LoadResourceProxy.getXMLStringPerPage(LoadResourceProxy.PRODUCT_LIST,companyId,page,offset);
    	
		XmlPullParser xrp=Xml.newPullParser();

		if(xmlStr==null){
			
			throw new Exception("appear error");
		}
		
		StringReader reader=new StringReader(xmlStr);
		
		xrp.setInput(reader);
		
		boolean isProduct=false;
		Product p=null;
			
		while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
					
		   if(xrp.getEventType()==XmlResourceParser.START_TAG){
						
			 String tagName=xrp.getName();
			       
			 if(tagName.equalsIgnoreCase("product")){
							
					isProduct=true;
					p=new Product(companyId);
					       
			 }else if(tagName.equalsIgnoreCase("id")&&isProduct){
					   
				  p.setId(xrp.nextText());
					   
			 }else if(tagName.equalsIgnoreCase("title")&&isProduct){
					   	
				 p.setTitle(xrp.nextText());
				 
			 }else if(tagName.equalsIgnoreCase("image")&&isProduct){
				   
				   p.getImageList().add(xrp.nextText());
			  
			 }else if(tagName.equalsIgnoreCase("rating")&&isProduct){
				   
				   p.setRate(Float.valueOf(xrp.nextText()));
			  }
		   
		   }else if(xrp.getEventType()==XmlResourceParser.END_TAG){
				   
				   String tagName=xrp.getName();
				   
				   if(tagName.equalsIgnoreCase("product")){
						
					   isProduct=false;
					   plist.add(p);
				   }
			}
					
		  xrp.next();
		  
	   }
	
		return plist;
	}
	
	

}
